package com.hluck.calculatertest.ui.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.activityViewModels
import androidx.navigation.Navigation
import com.hluck.calculatertest.R
import com.hluck.calculatertest.databinding.FragmentQuestionBinding
import com.hluck.calculatertest.ui.viewmodels.MyViewModel
import java.lang.StringBuilder

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [QuestionFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class QuestionFragment : Fragment() {

    private val viewModel: MyViewModel by activityViewModels()
    private lateinit var mBinding: FragmentQuestionBinding
    private val answers: StringBuilder = StringBuilder()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_question, container, false)
        mBinding.viewmodel = viewModel
        mBinding.lifecycleOwner = activity
        viewModel.generator()

        val listener = View.OnClickListener {
            when (it.id) {
                mBinding.btn1.id -> {
                    answers.append("1")
                }

                mBinding.btn2.id -> {
                    answers.append("2")
                }

                mBinding.button3.id -> {
                    answers.append("3")
                }

                mBinding.button4.id -> {
                    answers.append("4")
                }

                mBinding.button5.id -> {
                    answers.append("5")
                }

                mBinding.button6.id -> {
                    answers.append("6")
                }

                mBinding.button7.id -> {
                    answers.append("7")
                }

                mBinding.button8.id -> {
                    answers.append("8")
                }

                mBinding.button9.id -> {
                    answers.append("9")
                }

                mBinding.button10.id -> {
                    answers.setLength(0)
                }

                mBinding.button11.id -> {
                    answers.append("0")
                }
            }
            if (answers.isEmpty()) {
                mBinding.tvGuess.text = getString(R.string.your_answer)
            } else {
                mBinding.tvGuess.text = answers.toString()
            }
        }

        mBinding.apply {
            btn1.setOnClickListener(listener)
            btn2.setOnClickListener(listener)
            button3.setOnClickListener(listener)
            button4.setOnClickListener(listener)
            button5.setOnClickListener(listener)
            button6.setOnClickListener(listener)
            button7.setOnClickListener(listener)
            button8.setOnClickListener(listener)
            button9.setOnClickListener(listener)
            button10.setOnClickListener(listener)
            button11.setOnClickListener(listener)
        }


        mBinding.button12.setOnClickListener {
            if (answers.toString().toInt() == viewModel.answer().value) {
                viewModel.answerCorrect()
                answers.setLength(0)
                mBinding.tvGuess.text = getString(R.string.answer_correct)
            } else {
                val controller = Navigation.findNavController(it)
                if (viewModel.isWin) {
                    controller.navigate(R.id.action_questionFragment_to_winFragment)
                    viewModel.isWin = false
                    viewModel.saveHighScore()
                } else {
                    controller.navigate(R.id.action_questionFragment_to_failFragment)
                }
            }
        }
        return mBinding.root
    }


}